<?php

class OptionsController extends BackendController
{
    /**
     * 网站配置信息
     */
    public function actionWebsite()
    {
        $model=new WebSettingForm();
        $model->getWebsiteSetting();

        if(Yii::app()->request->isPostRequest && $model->validate() && $model->setWebsiteConfig() ){

            Yii::app()->user->setFlash('success', Yii::t('app', 'Update Success'));
            $this->redirect(array('website'));
        }
        $this->render('website',
            array('model'=>$model)
        );
    }

    /**
     * SMTP配置
     */
    public function actionSmtp()
    {
        $model=new SMTPForm();
        $model->getSmtpConfig();
        if(Yii::app()->request->isPostRequest && $model->validate() && $model->setSmtpConfig() ){
            Yii::app()->user->setFlash('success', Yii::t('app', 'Update Success'));
            $this->redirect(array('smtp'));
        }

        $this->render('smtp',
            array('model'=>$model)
        );
    }

    /**
     * @return false|string|string[]|null
     * @throws CException
     */
    public function actionCustom()
    {
        $settings = Options::model()->findAll(array(
            'order'=>'sort ASC,id ASC',
            'condition'=>'type = :type',
            'params'=>array(':type'=>Options::TYPE_CUSTOM),
        ));
        if (Yii::app()->request->isPostRequest) {
            foreach ($settings as $key=>$setting) {
                $custom = Options::model()->find('id=:id',array('id'=>$setting->attributes['id']));
                if($custom){
                    $model=Options::model()->updateAll(array('value'=>''.$_POST['Options'][''.$setting->attributes['id'].'']['value'].''),'id=:id',array('id'=>$setting->attributes['id']));
                }
            }
            Yii::app()->user->setFlash('success', Yii::t('app', 'Update Success'));
            $this->redirect(array('custom'));
        }
        return $this->render('custom', [
            'settings' => $settings,
        ]);
    }

    /**
     * 创建自定义设置
     */
    public function actionCustomCreate()
    {
        $model=new Options;
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);
        if(isset($_POST['Options']))
        {
            $model->attributes=$_POST['Options'];
            if($model->save())
                Yii::app()->user->setFlash('success', Yii::t('app', 'Create Success'));
                $this->redirect(array('custom'));
        }

        $this->render('custom_create',array(
            'model'=>$model,
        ));
    }

    /**
     * @param $id
     * @throws CDbException
     * @throws CHttpException
     */
    public function actionCustomDelete($id)
    {
        $this->loadModel($id)->delete();
        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if(!isset($_GET['ajax']))
            Yii::app()->user->setFlash('success', Yii::t('app', 'Delete Success'));
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('custom'));
    }

    /**
     * Upload配置
     */
    public function actionUpload()
    {
        $model=new UploadForm();
        $model->getUploadConfig();
        if(Yii::app()->request->isPostRequest && $model->validate() && $model->setUploadConfig() ){
            Yii::app()->user->setFlash('success', Yii::t('app', 'Update Success'));
            $this->redirect(array('upload'));
        }

        $this->render('upload',
            array('model'=>$model)
        );
    }


	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Options the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Options::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Options $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='options-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
